<?php

namespace app\admin\controller;

use think\Db;
use think\facade\Request;

class System extends Common
{
    //站点设置
    public function system()
    {
        if (Request::isAjax()) {
            $data = input('post.');
            foreach ($data as $k => $v) {
                Db::name('config')->where([['name', '=', $k], ['type', '=', 'site']])->update(['value' => $v]);
            }
            return json(['code' => 1, 'msg' => '网站设置成功!', 'url' => url('system/system')]);
        } else {
            $info = Db::name('config')->where('type', 'site')->where('status',1)->select();
            $info = convert_arr_kv($info, 'name', 'value');
            $this->assign('info', json_encode($info, true));
            return $this->fetch();
        }
    }
    
    public function email()
    {
        if (Request::isAjax()) {
            $datas = input('post.');
            foreach ($datas as $k => $v) {
                Db::name('config')->where([['name', '=', $k], ['type', '=', 'smtp']])->update(['value' => $v]);
            }
            return json(['code' => 1, 'msg' => '邮箱设置成功!', 'url' => url('system/email')]);
        } else {
            $smtp = Db::name('config')->where('type', 'smtp')->where('status',1)->select();
            $info = convert_arr_kv($smtp, 'name', 'value');
            $this->assign('info', json_encode($info, true));
            return $this->fetch();
        }
    }
    
    public function trySend()
    {
        $sender = input('email');
        //检查是否邮箱格式
        if (!is_email($sender)) {
            return json(['code' => 0, 'msg' => '测试邮箱码格式有误']);
        }
        $arr = db('config')->where('type', 'smtp')->select();
        $config = convert_arr_kv($arr, 'name', 'value');
        $content = $config['test_eamil_info'];
        $send = send_email($sender, '测试邮件', $content);
        if ($send) {
            return json(['code' => 1, 'msg' => '邮件发送成功！']);
        } else {
            return json(['code' => 0, 'msg' => '邮件发送失败！']);
        }
    }
    
}
